<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="20">
        <span style="margin-right: 10px">【{{$route.query.name}}】课程视频</span>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:CompassVideoList:add']"
        >新增视频
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CompassVideoListList"
              @cell-dblclick="cellDbClick"
              @selection-change="handleSelectionChange">
      <el-table-column label="名称" align="center" prop="videoName"/>
      <el-table-column label="时长（分）" align="center" prop="videoDuration"/>
      <el-table-column label="是否付费" align="center" prop="isPaid">
        <template #default="{row}">{{row.isPaid ? '付费' : '免费'}}</template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort">
        <template #default="{row}">
          <span v-if="!row.edit">{{ row.sort }}</span>
          <el-input type="number" v-else v-model="row.sort" @blur="saveSort(row)" autocomplete="on" ref="input"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:CompassVideoList:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:CompassVideoList:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程视频列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="视频名称" prop="videoName">
          <el-input v-model="form.videoName" placeholder="请输入视频名称"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" type="number" placeholder="排序"/>
        </el-form-item>

        <el-form-item label="视频时长" prop="videoDuration">
          <el-input v-model="form.videoDuration" placeholder="请输入视频时长">
            <template slot="append">分钟</template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否付费" prop="isPaid">
          <el-switch
            v-model="form.isPaid"
            :active-value="1"
            :inactive-value="0"
            active-text="付费"
            inactive-text="免费">
          </el-switch>
        </el-form-item>
        <el-form-item label="视频类型" prop="videoUrl" label-width="90px">
          <el-radio-group v-model="form.videoType">
            <el-radio :label="1">本地上传</el-radio>
            <el-radio :label="2">输入视频地址</el-radio>
          </el-radio-group>
          <Upload v-model="form.videoUrl" type="video" v-if="form.videoType == 1"/>
          <el-input v-model="form.videoUrl" placeholder="请输入视频地址" v-if="form.videoType ==2"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompassVideoList,
  getCompassVideoList,
  delCompassVideoList,
  addCompassVideoList,
  updateCompassVideoList,
  exportCompassVideoList
} from '@/api/system/CompassVideoList'
import Editor from "@/components/Editor";
import Upload from "@/components/Upload";

export default {
  components: {
    Editor,
    Upload
  },
  name: 'CompassVideoList',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程视频列表表格数据
      CompassVideoListList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        videoId: this.$route.query.id,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        videoId: [
          { required: true, message: '视频ID不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    saveSort(row) {
      row.edit = false
      updateCompassVideoList(row).then(response => {
        if (response.code === 200) {
          this.msgSuccess('修改成功')
        }
      })
    },

    cellDbClick(row, column) {
      if (column.property === 'sort') {
        this.$set(row, 'edit', true)
        this.$nextTick(() => {
          this.$refs.input.focus()
        })
      }
    },

    /** 查询课程视频列表列表 */
    getList() {
      this.loading = true
      listCompassVideoList(this.queryParams).then(response => {
        this.CompassVideoListList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sort: null,
        videoType: null,
        videoId: null,
        isPaid: null,
        videoDuration: null,
        videoUrl: null,
        videoName: null,
        updateTime: null,
        createTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加课程视频列表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCompassVideoList(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改课程视频列表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCompassVideoList(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            this.form.videoId = this.$route.query.id
            addCompassVideoList(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除课程视频列表编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCompassVideoList(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有课程视频列表数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportCompassVideoList(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>
